package org.jeecg.modules.controller;

import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.entity.DicXzqh;
import org.jeecg.modules.entity.InterfaceInfo;
import org.jeecg.modules.service.IDicXzqhService;
import org.jeecg.modules.service.IInterfaceInfoService;
import org.jeecg.modules.utils.ApiUtil;
import org.jeecg.modules.utils.UrlUtil;
import org.jeecg.modules.vo.ParamsVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.*;

/**
 * 生活服务
 * @Author: nc
 * @Date: 2023/2/3 14:24
 * @Description:
 */
@Api(tags="2.生活服务控制层")
@RestController
@RequestMapping("/boot/lifeController")
@Slf4j
public class LifeController {

    @Resource
    private IDicXzqhService dicXzqhService;

    @Resource
    private IInterfaceInfoService interfaceInfoService;

    /**
     * 实时油价查询
     *
     * @param request
     * @param paramsVo
     * @return
     */
    @ApiOperation("实时油价查询")
    @RequestMapping(value = "/oilPrice", method = RequestMethod.POST)
    public Result<?> oilPrice(HttpServletRequest request,@RequestBody ParamsVo paramsVo) {
        DicXzqh entity = dicXzqhService.getById(paramsVo.getProv());
        Map<String,String> analysisStr = new HashMap<>();
        Map<String, String> urlMap = UrlUtil.oilprice;
       try {
           Map<String , String> map = new HashMap<>();
           map.put("prov",entity.getShortname());
           //格式化查询条件
           String content = ApiUtil.formatMap(urlMap, map);
           //查询接口
           analysisStr = interfaceInfoService.selectInterfaceInfo(content, urlMap, map);
       }catch (Exception e){
           e.printStackTrace();
       }
        return Result.ok(analysisStr);
    }

    /**
     * 查询行政区划集合
     *
     * @param request
     * @param paramsVo
     * @return
     */
    @ApiOperation("查询行政区划集合")
    @RequestMapping(value = "/getXzqhList", method = RequestMethod.POST)
    public Result<?> getXzqhList(HttpServletRequest request,@RequestBody ParamsVo paramsVo) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<Map<String, Object>> xzqhList = dicXzqhService.getXzqhList(paramsVo.getXzqhId());
        for (Map<String, Object> map : xzqhList){
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("text",map.get("name"));
            resultMap.put("value",map.get("id"));
            List<Map<String, Object>> list = dicXzqhService.getXzqhList(map.get("id").toString());
            List<Map<String, Object>> childrenList = new ArrayList<>();
            for (Map<String, Object> childrenMap : list){
                Map<String, Object> children = new HashMap<>();
                children.put("text",childrenMap.get("name"));
                children.put("value",childrenMap.get("id"));
                childrenList.add(children);
            }
            resultMap.put("children",childrenList);
            resultList.add(resultMap);
        }
        return Result.ok(resultList);
    }

}
